/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobscheduling.algorithms.pso;

import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import jobscheduling.algorithms.AbstractScheduler;
import jobscheduling.algorithms.ResultListener;
//import jobscheduling.algorithms.bee.BeeScheduler;

/**
 *
 * @author Dawid
 */
public class PSOScheduler extends AbstractScheduler {
    private static final Map<String, String> _params = new TreeMap<String, String>();
    private static final Map<String, String> _descs = new TreeMap<String, String>();
    
    static {
        _params.put("iterations", "1000");
        _params.put("inertiaWeight", "1.0");
        _params.put("cognitionLearmningFactor", "1.0");
        _params.put("socialLearningFactor", "1.0");
        _params.put("maxVelocity", "4.0");
        _params.put("particlesNumber", "10");
        
        _descs.put("iterations", "liczba iteracji");
        _descs.put("inertiaWeight", "waga bezwladnosci");
        _descs.put("cognitionLearmningFactor", "wspolczynnik poznawczy");
        _descs.put("socialLearningFactor", "wspolczynnik spoleczny");
        _descs.put("maxVelocity", "gorne ograniczenie predkosci");
        _descs.put("particlesNumber", "liczba czasteczek");
    }
    
    public PSOScheduler (int[][] jobList, ResultListener listener, 
                         Map<String, String> params){
        super(jobList, listener, params);
    }
    
    public static Map<String, String> getParamsPresets() {
        return _params;
    }

    public static Map<String, String> getParamsDescs() {
        return _descs;
    }

    @Override
    public boolean isCorrect(String parameter, String value) {
        return true;
    }
    
    private void prepare() {
    	
    	int iterations = Integer.parseInt(_parameters.get("iterations"));
    	
    	int machinesNo = _jobs.length;
    	int jobsNo = _jobs[0].length;
    	int lczastek = Integer.parseInt(_parameters.get("particlesNumber"));
    	float inertiaWeight = Float.parseFloat(_parameters.get("inertiaWeight"));
    	float cognitionLearmningFactor = Float.parseFloat(_parameters.get("cognitionLearmningFactor"));
    	float socialLearningFactor = Float.parseFloat(_parameters.get("socialLearningFactor"));
    	float maxVelocity = Float.parseFloat(_parameters.get("maxVelocity"));
    	
    	Evaluator evaluator = new Evaluator(new InputData(machinesNo, jobsNo, _jobs));

		Swarm s = new Swarm(jobsNo, lczastek, evaluator);
		/*liczba zadań, liczba cząsteczek, obiekt obliczajacy czas rozwiazania*/
		
		s.setInertiaWeight(inertiaWeight);
		s.setCognitionLearmningFactor(cognitionLearmningFactor);
		s.setSocialLearningFactor(socialLearningFactor);
		s.setMaxVelocity(maxVelocity);
		/*waga bezwładości, współczynnik poznawczy, współczynnik społeczny, górne ograniczenie prrędkości*/
		
		int[] res = s.seekSolution(iterations);	/*liczba iteracji*/
		
		System.out.println("Rozwiazanie:");
		for(int y = 0; y<res.length; y++)
			System.out.println("Zadanie numer " + y + ": " + res[y]);
	}
    
    @Override
    public void run() {
    	
    	prepare(); //adaptacja do istniejacej klasy Swarm
    	
        int[] permutation = new int[_jobs[0].length];
        for (int i = 0; i < permutation.length; ++i){
            permutation[i] = i;
        }
        int iterations = Integer.parseInt(_params.get("iterations"));
        for (int i = 0; i < iterations; ++i){
            try {
                Thread.sleep(1);
            } catch (InterruptedException ex) {
                //Logger.getLogger(BeeScheduler.class.getName()).log(Level.SEVERE, null, ex);
            }
            this._result.newResult(permutation, 11000 - i);
        }
    }
}
